package com.szt.mobile.inventory.modules.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.szt.mobile.inventory.R;
import com.szt.mobile.inventory.common.constants.IntfConstants;
import com.szt.mobile.inventory.common.entity.Account;
import com.szt.mobile.inventory.common.entity.AccountBean;
import com.szt.mobile.inventory.common.entity.Login;
import com.szt.mobile.inventory.common.entity.User;
import com.szt.mobile.inventory.common.ui.BaseActivity;
import com.szt.mobile.inventory.common.utils.CacheUtil;
import com.szt.mobile.inventory.common.utils.DBUtil;
import com.szt.mobile.inventory.common.utils.GsonUtil;
import com.szt.mobile.inventory.common.utils.Network;
import com.szt.mobile.inventory.common.utils.SpUtil;
import com.szt.mobile.inventory.common.utils.ToastUtils;
import com.szt.mobile.inventory.common.utils.Xutils;
import com.szt.mobile.inventory.modules.login.biz.LoginBiz;
import com.szt.mobile.inventory.modules.main.ui.MainActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yanzhaodi on 2017/9/12.
 */
@SuppressWarnings("unchecked")
public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_login_username)
    EditText usernameEt;
    private Login mLogin;
    @BindView(R.id.et_login_pwd)
    EditText pwdEt;
    AccountBean accountList;
    List<Account> mAccounts;
    private SpUtil spUtil;

    private String username;

    private Xutils mXutils;
    private boolean isLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mXutils = Xutils.getInstance();
        spUtil = SpUtil.getInstance(getApplicationContext());
        String username = spUtil.getKeyStringValue(SpUtil.Key.KEY_USERNAME, "");
        String pwd = spUtil.getKeyStringValue(SpUtil.Key.KEY_PASSWORD, "");

        usernameEt.setText(username);
        pwdEt.setText(pwd);
    }

    public void login(View view) {
        // 必须先配置了ip和端口才能登陆
        if (TextUtils.isEmpty(IntfConstants.IP)) {
            showToast("请先设置IP和端口");
            return;
        }

        username = usernameEt.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            showToast("请输入用户名");
            return;
        }

        final String pwd = pwdEt.getText().toString();

        if (Network.isOpenNetwork(LoginActivity.this)==true){
            showLoading();
            LoginBiz.login(mXutils, username, pwd, new Xutils.XCallBack() {
                @Override
                public void onResponse(String result) {
                    mLogin= GsonUtil.GsonToBean(result,Login.class);
                    if (mLogin.getData().getResultCode().equals("2")){
                        hideLoading();
                        isLogin=spUtil.getState();
                        if (!isLogin){
                            spUtil.saveKeyStringValue(SpUtil.Key.KEY_USERNAME, username);
                            spUtil.saveKeyStringValue(SpUtil.Key.KEY_PASSWORD, pwd);
                            CacheUtil.setCacheUserName(username);
                            loadAccount();
                        }else {
                            User user = new User();
                            user.username = username;
                            user.password = pwd;
                            List<Account> accountList = DBUtil.getAccountDao(getApplicationContext()).loadAll();
                            if (accountList == null || accountList.isEmpty()) {
                                showToast("网络异常");
                            }else {
                                Account account = new Account();
                                account.setUid(user.username);
                                account.setPwd(user.password);
                                if (accountList.contains(account)) {
                                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                                    finish();
                                } else {
                                    showToast("用户名或密码错误");
                                }
                            }
                        }
                    }else {
                        hideLoading();
                        showToast("用户名或密码错误");
                    }
                }

                @Override
                public void onError(String e) {
                    hideLoading();
                    showToast(e);

                }
            });
        }else {
            showToast("网络连接失败");
        }
    }

    private void loadAccount() {
        LoginBiz.loadAccount(mXutils, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                accountList= GsonUtil.GsonToBean(result,AccountBean.class);
                mAccounts=accountList.getData();
                if (mAccounts==null){
                    showToast("同步用户失败");
                }else {
                    DBUtil.getAccountDao(getApplicationContext()).insertInTx(mAccounts);
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                    finish();
                }
                hideLoading();
            }

            @Override
            public void onError(String e) {
                hideLoading();
                showToast(e);
            }
        });
    }

    public void configManager(View view) {
        startActivity(new Intent(this, ConfigManagerActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
